﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WCSY.DAL
{
    public class fg_projService
    {
        public fg_projService()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DBHelper.GetMaxID("id", "fg_proj"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from fg_proj");
			strSql.Append(" where id="+id+" ");
			return DBHelper.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public string Add(WCSY.MODEL.fg_proj model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.id != null)
			{
				strSql1.Append("id,");
				strSql2.Append(""+model.id+",");
			}
			if (model.projNo != null)
			{
				strSql1.Append("projNo,");
				strSql2.Append("'"+model.projNo+"',");
			}
			if (model.projName != null)
			{
				strSql1.Append("projName,");
				strSql2.Append("'"+model.projName+"',");
			}
			strSql.Append("insert into fg_proj(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			return strSql.ToString();
			
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public string Update(WCSY.MODEL.fg_proj model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update fg_proj set ");
			if (model.projNo != null)
			{
				strSql.Append("projNo='"+model.projNo+"',");
			}
			else
			{
				strSql.Append("projNo= null ,");
			}
			if (model.projName != null)
			{
				strSql.Append("projName='"+model.projName+"',");
			}
			else
			{
				strSql.Append("projName= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where id="+ model.id+" ");
			return strSql.ToString();
			
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public string Delete(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fg_proj ");
			strSql.Append(" where id="+id+" " );
			return strSql.ToString();
			
		}		/// <summary>
		/// 批量删除数据
        /// </summary>projInfo
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fg_proj ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DBHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public WCSY.MODEL.fg_proj GetModel(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" id,projNo,projName ");
			strSql.Append(" from fg_proj ");
			strSql.Append(" where id="+id+" " );
			WCSY.MODEL.fg_proj model=new WCSY.MODEL.fg_proj();
			DataSet ds=DBHelper.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["projNo"]!=null && ds.Tables[0].Rows[0]["projNo"].ToString()!="")
				{
					model.projNo=ds.Tables[0].Rows[0]["projNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["projName"]!=null && ds.Tables[0].Rows[0]["projName"].ToString()!="")
				{
					model.projName=ds.Tables[0].Rows[0]["projName"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,projNo,projName ");
			strSql.Append(" FROM fg_proj ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,projNo,projName ");
			strSql.Append(" FROM fg_proj ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM fg_proj ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DBHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from fg_proj T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DBHelper.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
    }
}
